import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'
import { Message } from 'element-ui'
import common from '../utils/common.js'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const flat = (arr, menulist) => {
  menulist.forEach(item => {
    if (Array.isArray(item.highestRoleMenuVos)) {
      if (item.highestRoleMenuVos.length > 0) {
        flat(arr, item.highestRoleMenuVos)
      } else {
        arr.push(item)
      }
    } else {
      arr.push(item)
    }
  })
  return arr
}

// let menuRouter=JSON.parse(localStorage.getItem('menuRouter'));
const routes = [
  {
    path: '/',
    component: () => import('../views/index/index'),
    meta: { title: '主页' },
    redirect: '/index/biddingPlaza',
    children: [
      {
        path: '/index/biddingPlaza',
        component: () => import('../components/BiddSquare.vue'),
        meta: { title: '招标广场' }
      },
      {
        path: '/index/biddingDetail/:id',
        component: () => import('../views/index/biddDetail.vue'),
        meta: { title: '招标详情' }
      },
      {
        path: '/index/supplier',
        component: () => import('../views/index/homeSupplier.vue'),
        meta: { title: '供应商', ignore: true }
      },
      {
        name: 'SupplierDetail',
        path: '/index/supplierDetail',
        component: () => import('../views/index/supplierDetail.vue'),
        meta: { title: '供应商详情', ignore: true }
      }
    ]
  },
  {
    path: '/upcoming',
    component: () => import('../views/upcoming'),
    meta: { title: '待办事项' }
  },
  {
    path: '/personal/index',
    component: () => import('../views/personal/index'),
    meta: { title: '个人中心' },
    // redirect:menuRouter===null?"/login":menuRouter[0]["highestRoleMenuVos"][0]["path"],
    children: [
      {
        path: '/personal/roleManage',
        component: () => import('../views/personal/roleManage'),
        meta: { title: '角色管理' }
      },
      {
        path: '/personal/material',
        component: () => import('../views/personal/materialManage'),
        meta: { title: '材料管理' }
      },
      {
        path: '/personal/product',
        component: () => import('../views/personal/mainProducts'),
        meta: { title: '主营产品' }
      },
      {
        path: '/personal/biddManage',
        component: () => import('../views/personal/biddManage'),
        meta: { title: '招标管理' }
      },
      {
        path: '/personal/userManage',
        component: () => import('../views/personal/userManage'),
        meta: { title: '用户管理' }
      },
      {
        path: '/personal/audits',
        component: () => import('../views/personal/workManage'),
        meta: { title: '各项审核' }
      },
      {
        path: '/personal/projectManage',
        component: () => import('../views/personal/projectManage'),
        meta: { title: '工程管理' }
      },
      {
        path: '/personal/accountManage',
        component: () => import('../views/personal/accountManage'),
        meta: { title: '入账管理' }
      },
      {
        path: '/personal/invoiceManage',
        component: () => import('../views/personal/InvoiceManage'),
        meta: { title: '发票管理' }
      },
      {
        path: '/personal/sealManage',
        component: () => import('../views/personal/sealManage'),
        meta: { title: '印章管理' }
      },
      {
        path: '/personal/tenderManage',
        component: () => import('../views/personal/tenderManage'),
        meta: { title: '投标管理' }
      },
      {
        path: '/personal/contractManage',
        component: () => import('../views/personal/contractManage'),
        meta: { title: '合同管理' }
      },
      {
        path: '/personal/contractOrder',
        component: () => import('../views/personal/contractOrderManage'),
        meta: { title: '合同订单管理' }
      },
      {
        path: '/personal/orderSettlement',
        component: () => import('../views/personal/orderSettlementManage'),
        meta: { title: '合同结算管理' }
      },
      {
        path: '/personal/information',
        component: () => import('../views/personal/InformationMaintenance'),
        meta: { title: '企业信息维护' }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('../views/login'),
    meta: { title: '登录' }
  },
  {
    path: '/register',
    component: () => import('../views/register'),
    meta: { title: '注册' }
  },
  {
    path: '/retrieve',
    component: () => import('../views/retrieve'),
    meta: { title: '找回密码' }
  },
  {
    path: '/perfectInfo',
    component: () => import('../views/perfectInfo'),
    meta: { title: '完善信息' }
  },
  {
    path: '/404',
    component: () => import('../views/404'),
    meta: { title: '404' }
  },
  {
    path: '*',
    redirect: '/404'
  }
]

const router = new VueRouter({
  mode: 'hash',
  routes
})

router.beforeEach(async function (to, from, next) {
  if (to.meta.ignore) {
    next()
    return
  }
  let isLogin = store.state.whetherLogin
  // 是否登录
  if (isLogin) {
    if (!localStorage.getItem('userId')) {
      common.setCookie('materialsToken', '', 1)
    }
    store.dispatch('getUpcoming')
    // 所有人都可以前往首页以及待办事项
    if (to.path === '/' || to.path === '/index/biddingPlaza' || to.path === '/upcoming' || to.path === '/perfectInfo') {
      next()
    } else {
      // 如果路由前往招标详情
      if (to.meta.title === '招标详情' && to.path.split('/index/biddingDetail/')[1]) {
        next()
      } else {
        let menulist = [...store.state.personalMenuList]
        let arr = []
        arr = [...flat(arr, menulist)]
        if (!arr.length) {
          if (to.path === '/personal/index') {
            Message.error('未拥有个人中心权限')
            next(false)
          } else {
            if (
              to.path === '/login' ||
              to.path === '/register' ||
              to.path === '/retrieve' ||
              to.path === '/perfectInfo'
            ) {
              next('/')
            } else {
              next()
            }
          }
        } else {
          if (
            to.path === '/login' ||
            to.path === '/register' ||
            to.path === '/retrieve' ||
            to.path === '/perfectInfo'
          ) {
            next(false)
          } else {
            if (to.path === '/personal/index') {
              next()
            } else {
              let flag = arr.some(item => {
                return item.path === to.path
              })
              if (flag) {
                next()
              } else {
                next(false)
              }
            }

            // next()
          }
        }
      }
    }
  } else {
    if (
      to.path == '/' ||
      to.path === '/login' ||
      to.path === '/register' ||
      to.path === '/retrieve' ||
      // to.path === '/perfectInfo' ||
      to.path === '/upcoming' ||
      to.path === '/index/biddingPlaza'
    ) {
      next()
    } else {
      if (to.meta.title === '招标详情' && to.path.split('/index/biddingDetail/')[1]) {
        next()
      } else {
        next('/login')
      }
    }
  }
})
export default router
